package com.task.sql.schedule.util.db;/**
 * @ClassName : SqlTypeSwitch
 * @Description : sql类型判别
 * @Author : liudonghai
 * @Date: 2024-09-11 17:44
 */

import com.task.sql.schedule.constant.DBOperaterValue;
import org.apache.commons.lang3.StringUtils;

import java.util.Locale;

/**
 * @ClassName : SqlTypeSwitch
 * @Description : sql类型判别
 * @Author : liudonghai
 * @Date: 2024-09-11 17:44
 */

public class SqlTypeSwitch {
    /**
     * 判断SQL操作类型
     * @param sql
     * @return DBOperaterValue的OP值
     */
    public static int switchSqlType(String sql){
        if(StringUtils.isEmpty(sql)){return DBOperaterValue.OP_NULL.getCode();}
        String newSql = sql.toUpperCase(Locale.ROOT).trim();
        if(newSql.indexOf(DBOperaterValue.OP_SELECT.getMsg())==0){return DBOperaterValue.OP_SELECT.getCode();}
        if(newSql.indexOf(DBOperaterValue.OP_INSERT.getMsg())==0){return DBOperaterValue.OP_INSERT.getCode();}
        if(newSql.indexOf(DBOperaterValue.OP_DELETE.getMsg())==0){return DBOperaterValue.OP_DELETE.getCode();}
        if(newSql.indexOf(DBOperaterValue.OP_UPDATE.getMsg())==0){return DBOperaterValue.OP_UPDATE.getCode();}
        return DBOperaterValue.OP_NULL.getCode();
    }
}
